<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/5 0005
 * Time: 16:26
 */

namespace app\admin\controller;


use think\Request;

class Salesman extends Base
{
    protected $_Salesman;

    public function __construct(Request $request = null){
        parent::__construct($request);
        $this->_Salesman = new \app\model\model\Salesman();

    }

    /*=======================客户来访===========================*/
    public function lists(){
        return $this->fetch();
    }
    public function getList(){
        $where = [];
        $name = input('name');
        if($name){
            $where['name'] = ['like',"%$name%"];
        }
        $data = $this->_Salesman->getList($where,input('limit'));
        foreach ($data as $v){
            $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
        }
        $list = $data->toArray();
        echo json_echo($list);
    }
    /**
     * 添加
     * */
    public function add(){
        if(!Request::instance()->post()){
            return $this->fetch();
        }else{

            $data = [
                'name'=>input('name'),
                'mobile'=>input('mobile'),
                'email'=>input('email'),
                'fax'=>input('fax'),
                'branch'=>input('branch'),
                'create_time'=>time(),
            ];
            $re = $this->_Salesman->add($data);
            if($re){
                return json_echo_msg(200,'添加成功');
            }else{
                return json_echo_msg(500,'添加失败');
            }
        }
    }
    /**
     * 修改
     * */
    public function up(){
        if(!Request::instance()->post()){
            $data = $this->_Salesman->getOne(input('id'));
            $this->assign('data',$data);
            return $this->fetch();
        }else{
            $data = [
                'name'=>input('name'),
                'mobile'=>input('mobile'),
                'email'=>input('email'),
                'fax'=>input('fax'),
                'branch'=>input('branch'),
            ];
            if(input('img_url')){
                $data['img_url'] = input('img_url');
            }

            $re = $this->_Salesman->up(['id'=>input('id')],$data);
            if($re){
                return json_echo_msg(200,'修改成功');
            }else{
                return json_echo_msg(500,'修改失败');
            }
        }
    }
    /**
     * 获取一条数据
     * */
    public function getOne(){
        $data = $this->_Salesman->getOne(input('id'));
        echo json_echo_msg(200,'成功',$data);
    }
    /**
     * 完全删除
     * */
    public function del(){
        $re = $this->_Salesman->del(['id'=>input('id')]);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }
    /**
     * 批量完全删除
     * */
    public function delAll(){
        $re = $this->_Salesman->delAll(input()['data']);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }
 
}